package huoye.shops.huoye.operation.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.codeideology.android.lang.entity.Paging;
import com.codeideology.android.widget.recycler.RecyclerViewAdapter;

import butterknife.BindView;
import huoye.shops.R;
import huoye.shops.base.BaseActivity;
import huoye.shops.base.PullToRefreshFragment;
import huoye.shops.huoye.mine.model.ProductInfo;
import huoye.shops.huoye.operation.activity.LimitDiscountActivity;
import huoye.shops.huoye.operation.activity.OrderDetailsActivity;
import huoye.shops.huoye.operation.adapter.ManageShopAdapter;
import huoye.shops.huoye.operation.adapter.SellOrderAdapter;
import huoye.shops.huoye.operation.model.SellOrder;
import huoye.shops.huoye.operation.model.ShopProductList;
import huoye.shops.huoye.operation.presenter.ManageProductPresenter;
import huoye.shops.huoye.operation.presenter.SellOrderPresenter;
import huoye.shops.huoye.operation.view.IManageProductRefreshView;
import huoye.shops.huoye.operation.view.ISellOrderRefreshView;
import huoye.shops.widget.CommonHintDialog;


/**
 * 2018 - 08 - 10 .
 */
public class ManageProductFragment extends PullToRefreshFragment<ShopProductList> implements IManageProductRefreshView {


    public static final int TYPE_MANAGE_PRODUCT_ALL = 1; //店铺商品
    public static final int TYPE_MANAGE_PRODUCT_HKLM = 2; //黑卡/联盟商品
    public static final int TYPE_MANAGE_PRODUCT_XSZK = 3; //限时折扣
    public static final int TYPE_MANAGE_PRODUCT_NEW_USER = 4; //新人商品

    private int mType;
    private ManageProductPresenter mManageProductPresenter;
    private ManageShopAdapter adapter;

  @BindView(R.id.operation_manage_et_seek)
    EditText etSeek;




    @Override
    public RecyclerViewAdapter createAdapter() {

        Bundle bundle = getArguments();
        mType = bundle.getInt(BaseActivity.EXTRAS_KEY_DATA);
        setLayoutManager();
        adapter = new ManageShopAdapter(getActivity(),mType);
        return adapter;

    }

    @Override
    protected void initView(View view, @Nullable Bundle bundle) {
        super.initView(view, bundle);
        mManageProductPresenter = new ManageProductPresenter();
        mManageProductPresenter.addTaskListener(this);
        mManageProductPresenter.onStart();
        query();
        initHome();

    }


    @Override
    public int getContentViewId() {
        return R.layout.fragment_manage_product;
    }


    private void initHome(){
        etSeek.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent keyEvent) {
                /*判断是否是“GO”键*/
                if((actionId == 0 || actionId == 3) && keyEvent != null){
                    /*隐藏软键盘*/
                    InputMethodManager imm = (InputMethodManager) v
                            .getContext().getSystemService(
                                    Context.INPUT_METHOD_SERVICE);
                    if (imm.isActive()) {
                        imm.hideSoftInputFromWindow(
                                v.getApplicationWindowToken(), 0);
                    }

                    String seek = etSeek.getText().toString();
                    mManageProductPresenter.getProductList(seek,mType,new Paging());
                    return true;
                }
                return false;
            }
        });
    }




    @Override
    public void query(Paging paging) {
        super.query(paging);
        //商品管理
        mManageProductPresenter.getProductList("",mType,paging);
    }

    @Override
    public void onResume() {
        super.onResume();
        if(mManageProductPresenter != null){
            query();
        }
    }


    public static ManageProductFragment getInstan(int type) {
        ManageProductFragment fragment = new ManageProductFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(BaseActivity.EXTRAS_KEY_DATA, type);
        fragment.setArguments(bundle);
        return fragment;
    }




    @Override
    public void onItemClick(ShopProductList shopProductList, int i, View view) {
        mManageProductPresenter.getProductInfo(shopProductList.getProductId());
    }

    @Override
    public void getProductInfoData(ProductInfo productInfo) {
        productInfo.setTypeIntent(mType);
        startActivity(LimitDiscountActivity.class,productInfo);
    }
}
